package com.wanmait.translate.system.controller;

import com.wanmait.translate.dto.ResultData;
import com.wanmait.translate.pojo.SystemDept;
import com.wanmait.translate.pojo.SystemDict;
import com.wanmait.translate.pojo.SystemPost;
import com.wanmait.translate.utils.CommonMessage;
import com.wanmait.translate.utils.DictMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @Author 阿宇
 * @Description :组织结构
 * @Date 17:15 2020/10/30 0030
 */
@RestController
@RequestMapping("/system/post")
public class SystemPostController extends SystemParentController {

    private Logger log= LoggerFactory.getLogger(SystemPostController.class);

    /*
     * @Author huweibin
     * @Description 获得指定部门下辖的、指定条件的所有岗位
     * @Date 10:39 2020/11/12
     **/
    @RequestMapping("list")
    public ResultData<SystemPost>  list(SystemPost post){
        List<SystemPost> systemPosts=null;
        SystemPost systemPost = new SystemPost();
        systemPost.setId(0);
        if(post.getDeptId()==null){
            SystemDept systemDept = new SystemDept();
            systemDept.setId(0);
            post.setDeptId(systemDept);
        }
        if (post.getName().equals("")){
            post.setName(null);
        }
        //如果是顶级则为查询所有
        if (post.getDeptId().getId()==0){
            systemPosts = systemPostService.listByConditionAndDeptId(post);
            List<SystemDict> dictTypelist = systemDictService.getByDictTypeKey(DictMessage.DICT_COMPANT_NAME);
            SystemDict byDictTypeKey = dictTypelist.size()==0?null:dictTypelist.get(0);
            systemPost.setName(byDictTypeKey.getDictValue());
            systemPosts.add(systemPost);
        }else{
            systemPosts = systemPostService.listByConditionAndDeptId(post);
            SystemDept dept = systemDeptService.getByPrimaryKey(post.getDeptId().getId());
            //设置顶级岗位
            systemPost.setName(dept.getName());
            systemPosts.add(systemPost);
        }

        //log.info("部门id为"+deptId+"，返回的集合为"+systemPosts);
        return ResultData.SuccesssWithData(systemPosts);
    }
    @RequestMapping("save")
    public void save(SystemPost systemPost){
        if(systemPost.getId().equals(systemPost.getParentId())){
            return;
        }
        if (systemPost.getEnable()==null){
            systemPost.setEnable(1);
        }
        log.info("要保存的岗位对象"+systemPost);
        if (systemPost.getId()==null){
            systemPostService.save(systemPost);
        }else{
            systemPostService.update(systemPost);
        }

    }
        @RequestMapping("remove")
        public ResultData remove(Integer id){
        try {

            return systemPostService.remove(id);

        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.fail(CommonMessage.ERR_MSG);
        }
    }
}
